package ua.vn.ticket_scanner.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.View;
import android.widget.Button;

import ua.vn.ticket_scanner.R;
import ua.vn.ticket_scanner.TApplication;
import ua.vn.ticket_scanner.utils.TPreferenceManager;

/**
 * Created by Ivan on 22.06.13.
 */
public class SettingsActivity extends PreferenceActivity {

    private static final int MIN_TIMEOUT = 1000;

    private Button mDefaultButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
        setContentView(R.layout.preference_layout);

        mDefaultButton = (Button) findViewById(R.id.btn_default);
        mDefaultButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TPreferenceManager.getInstance().restoreDefaultParams();
                TApplication.getInstance().showToast(R.string.toast_default_params_were_set);
                Activity activity = SettingsActivity.this;
                activity.startActivity(new Intent(activity, SettingsActivity.class));
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();

        int timeout = TPreferenceManager.getInstance().getHttpTimeout();

        if(timeout < MIN_TIMEOUT) {
            timeout = MIN_TIMEOUT;
        }

        TPreferenceManager.getInstance().setHttpTimeout(timeout);
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDefaultButton = null;
    }
}